﻿using Charting;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PlotExample
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            float xMin = -180, xMax = 180, xStep = 30;
            float yMin = -150, yMax = -50, yStep = 20;
            float referValue = -107;
            int N = (int)((xMax - xMin) / xStep);

            float[] x = new float[N];
            float[] y = new float[N];
            float[] y2 = new float[N];
            float[] y3 = new float[N];
            Random random1 = new Random();
            for (int i = 0; i < N; i++)
            {
                x[i] = i * xStep - 180;
                int min = (int)(yMin + 0.5 * (yMax - yMin));
                y[i] = random1.Next(min, (int)yMax);
                y2[i] = random1.Next(min, (int)yMax);
                y3[i] = random1.Next(min, (int)yMax);
            }
            //
            // 直角坐标
            //
            plot1.AxisX.Minimum = xMin;   // X轴最小值
            plot1.AxisX.Maximum = xMax;   // X轴最大值
            plot1.AxisX.Interval = xStep;
            plot1.AxisY.Minimum = yMin;// Y轴最小值
            plot1.AxisY.Interval = yStep;
            plot1.AxisY.Maximum = yMax; // Y轴最大值
            plot1.ReferLineValue = referValue;
            plot1.ReferLineColor = Color.Red;
            plot1.PickEnabled = true;

            Charting.Series series = plot1.NewSeries();
            //series.LineColor = Color.Green;
            series.LineWidth = 1.8F;
            series.IsValueShownAsLabel = false;
            series.PointSize = 10;
            //series.PointColor = Color.Blue;
            //series.LabelBackColor = Color.Blue;

            series.BeginUpdate();
            for (int i = 0; i < N; i++)
            {
                series.Points.Add(x[i], y[i]);
            }
            series.EndUpdate();

            //line 2
            Charting.Series series2 = plot1.NewSeries();
            //series2.LineColor = Color.Red;
            series2.LineWidth = 1.8F;
            series2.IsValueShownAsLabel = false;
            series2.PointSize = 10;
            //series2.PointColor = Color.DarkRed;
            //series2.LabelBackColor = Color.DarkRed;
            series2.BeginUpdate();
            for (int i = 0; i < N; i++)
            {
                series2.Points.Add(x[i], y2[i]);
            }
            series2.EndUpdate();

            //line 3
            Charting.Series series3 = plot1.NewSeries();
            //series3.LineColor = Color.Yellow;
            series3.LineWidth = 1.8F;
            series3.IsValueShownAsLabel = false;
            series3.PointSize = 10;
            //series3.PointColor = Color.DarkRed;
            //series3.LabelBackColor = Color.DarkRed;
            series3.BeginUpdate();
            for (int i = 0; i < N; i++)
            {
                series3.Points.Add(x[i], y3[i]);
            }
            series3.EndUpdate();

        }

        private void Plot1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                IEnumerable<DataPoint> dataPoints = plot1.GetCursorData(e.X, e.Y, true);
                List<string> pStrs = new List<string>();
                foreach (var p in dataPoints)
                {
                    pStrs.Add($"x = {p.X}, y = {p.Y}");
                }
                listBox1.DataSource = pStrs;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Plot1_MouseMove(object sender, MouseEventArgs e)
        {
           
        }
    }
}
